

rdata_dir='D:\RawData';


user_dirs=dir(rdata_dir);
file_cnt=0;

time_stamps=[];
fract_running=[];

for ind=3:length(user_dirs)
    animal_dirs=dir([rdata_dir '\' user_dirs(ind).name]);
    for knd=3:length(animal_dirs)
        disp(['Now on ' animal_dirs(knd).name]);
        aux_files=dir([rdata_dir '\' user_dirs(ind).name '\' animal_dirs(knd).name '\*.lvd']);
        for jnd=1:length(aux_files)
            try
                tmp_data=load_lvd([rdata_dir '\' user_dirs(ind).name '\' animal_dirs(knd).name '\' aux_files(jnd).name]);
                file_cnt=file_cnt+1;
                velM=diff(tmp_data(5,:));
                velM(velM>5)=velM(velM>5)-10;
                velM(velM<-5)=velM(velM<-5)+10;
                %velM=ftfil(velM,1000,0,10);
                velM_ind=((smooth2(abs(velM),1000))>0.01);
                % before 2013 light cycle was 8:00 to 20:00 after it
                % switched to 2:00 to 14:00
                if aux_files(3).datenum>735234%=datenum(2012,12,31)
                    time_stamps(file_cnt)=rem(aux_files(jnd).datenum+0.25,1);
                else
                    time_stamps(file_cnt)=rem(aux_files(jnd).datenum,1);
                end
                fract_running(file_cnt)=sum(velM_ind)/length(velM_ind);
            end
        end
    end
end

bin_width=1/24;
bin_starts=[0:bin_width:1-bin_width];
bin_ends=bin_starts+bin_width;

bin_act=zeros(length(bin_starts),1);
bin_freq=zeros(length(bin_starts),1);
for ind=1:length(bin_starts)
    bin_act(ind)=mean(fract_running(time_stamps>bin_starts(ind)&time_stamps<bin_ends(ind)));
    bin_freq(ind)=sum((time_stamps>bin_starts(ind)&time_stamps<bin_ends(ind)));
end
figure;
plot(bin_act)

figure;
plot(bin_freq)

    
    
            
    